package com.xxl.job.executor.mvc.controller;

import com.xxl.job.client.TaskClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.biz.model.TaskInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/api")
public class IndexController {

    @Autowired
    private TaskClient taskClient;

    @ApiOperation(value = "创建任务")
    @PostMapping(value = "/create")
    public ReturnT create(@RequestBody TaskInfo info) {

        int task = taskClient.createTask(info);

        return new ReturnT(String.valueOf(task));
    }

    @ApiOperation(value = "启动任务")
    @GetMapping(value = "/start")
    public ReturnT start(@RequestParam String groupId, @RequestParam String handler) {

        taskClient.startTask(groupId, handler);

        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "启动多个任务")
    @GetMapping(value = "/start/list")
    public ReturnT startList(@RequestParam String groupId, @RequestParam List<String> handler) {

        taskClient.startTask(groupId, handler);

        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "创建任务并启动")
    @PostMapping(value = "/create/start")
    public ReturnT startTask(@RequestBody TaskInfo info) {

        int i = taskClient.createAndStart(info);

        return new ReturnT(String.valueOf(i));
    }

    @ApiOperation(value = "创建并启动多个任务")
    @PostMapping(value = "/create/start/list")
    public ReturnT createTask(@RequestBody List<TaskInfo> info) {

        taskClient.createAndStart(info);

        return new ReturnT();
    }

    @ApiOperation(value = "更新任务")
    @PostMapping(value = "/update")
    public ReturnT update(@RequestBody TaskInfo info) {

        taskClient.updateTask(info);

        return new ReturnT();
    }

    @ApiOperation(value = "更新多个任务")
    @PostMapping(value = "/update/list")
    public ReturnT updateList(@RequestBody List<TaskInfo> info) {

        taskClient.updateTask(info);

        return new ReturnT();
    }

    @ApiOperation(value = "停止任务")
    @GetMapping(value = "/stop/handler")
    public ReturnT stopHandler(@RequestParam String groupId, @RequestParam String handler) {

        taskClient.stopTask(groupId, handler);

        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "停止一组任务")
    @GetMapping(value = "/stop/group    ")
    public ReturnT stop(@RequestParam String groupId) {

        taskClient.stopTask(groupId);

        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "删除任务")
    @GetMapping(value = "/remove/handler")
    public ReturnT remove(@RequestParam String groupId, @RequestParam String handler) {

        taskClient.removeTask(groupId, handler);

        return ReturnT.SUCCESS;
    }

    @ApiOperation(value = "删除一组任务")
    @GetMapping(value = "/remove/group")
    public ReturnT remove(@RequestParam String groupId) {

        taskClient.removeTask(groupId);

        return ReturnT.SUCCESS;
    }

}